package com.cskaoyan.service.system;

import com.cskaoyan.bean.bo.system.ObjListBo;
import com.cskaoyan.bean.po.ohter.MarketStorage;
import com.cskaoyan.bean.po.ohter.MarketStorageExample;
import com.cskaoyan.bean.vo.system.ObjCreateVo;
import com.cskaoyan.bean.vo.system.ObjListInfo;
import com.cskaoyan.bean.vo.system.ObjListVo;
import com.cskaoyan.bean.vo.system.ObjUpdateVo;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.Constant;
import com.cskaoyan.util.common.StringUtils;
import com.cskaoyan.util.qcloudutils.QCloadManager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qcloud.cos.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: market
 * @description: 对象存储服务实现，实现类装入容器
 * @author: zhong
 * @create: 2022-07-17 18:13
 **/
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    MarketStorageMapper storageMapper;

    @Override
    public ObjListVo getObjListOp(ObjListBo pageReg) {
        PageHelper.startPage(pageReg.getPage(), pageReg.getLimit());

        MarketStorageExample example = new MarketStorageExample();
        example.setOrderByClause(pageReg.getSort() + " " + pageReg.getOrder());
        MarketStorageExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        //name模糊匹配
        String name = (StringUtils.isEmpty(pageReg.getName())) ? "%" : ("%" + pageReg.getName() + "%");
        criteria.andNameLike(name);
        //key完全匹配
        String key = pageReg.getKey();
        if (!StringUtils.isEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }

        List<MarketStorage> marketStorages = storageMapper.selectByExample(example);

        List<ObjListInfo> objInfos = new ArrayList<>();
        for (MarketStorage storage : marketStorages) {
            ObjListInfo info = new ObjListInfo();
            BeanUtil.copyBean(storage, info);
            objInfos.add(info);
        }

        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorages);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return new ObjListVo(total, pages, pageReg.getLimit(), pageReg.getPage(), objInfos);
    }

    @Override
    public ObjCreateVo addObjOp(MultipartFile file) throws IOException, ParseException {
        //直接使用流传入腾讯云，存本地服务器有系统文件路径不同导致的不兼容问题
        //file.transferTo(new File("D:\\tmp", file.getOriginalFilename()));
        InputStream in = file.getInputStream();
        //随机命名
        String uuid = UUID.randomUUID().toString();
        String hexStr = Integer.toHexString(file.hashCode());
        String objKey = "/pic/" + "image" + uuid + hexStr;
        //访问地址
        String url = Constant.QCLOUD_ACCESS_URL + objKey;
        //上传
        PutObjectResult upload = QCloadManager.upload(in, objKey);
        //存储时间
        //Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").parse(upload.getDateStr());
        System.out.println(upload.getDateStr());
        Date date = new Date();
        //存储信息存入数据库
        MarketStorage obj = new MarketStorage();
        //obj.setId();
        obj.setAddTime(date);
        obj.setUpdateTime(date);
        obj.setDeleted(false);
        obj.setSize(file.getSize());
        obj.setName(file.getOriginalFilename());
        obj.setType(file.getContentType());
        obj.setKey(objKey);
        obj.setUrl(url);
        storageMapper.insertReturnId(obj);

        //返回存储信息
        return storageMapper.selectById(obj.getId());
    }

    @Override
    public int delObjOp(int objId) {
        MarketStorage target = new MarketStorage();
        target.setId(objId);
        //逻辑删除，保留数据库记录，让开发者根据数据库删除图床对象
        target.setDeleted(true);
        int updateRow = storageMapper.updateByPrimaryKeySelective(target);
        if (updateRow != 1) {
            return Constant.FORBIDDEN_CODE;
        }
        return Constant.OK_CODE;
    }

    @Override
    public ObjUpdateVo changeNameOp(String newName, int objId) {
        MarketStorage target = new MarketStorage();
        target.setId(objId);
        target.setName(newName);
        storageMapper.updateByPrimaryKeySelective(target);
        MarketStorage marketStorage = storageMapper.selectByPrimaryKey(objId);
        ObjUpdateVo newInfo = new ObjUpdateVo();
        BeanUtil.copyBean(marketStorage, newInfo);
        return newInfo;
    }
}
